<template>
  <div class="login-container login-container-background">
    <div class="login-body">
      <img class="login-body-logo" alt="logo" src="../../assets/image/background_logo.png">
      <div class="login-body-form">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
          <span class="username-tips">User ID</span>

          <el-form-item prop="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              class="username"
              name="username"
              type="text"
              auto-complete="on"
              size="medium"
              maxlength="12"
              autofocus
            />
          </el-form-item>

          <span class="password-tips">Password</span>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              :type="pwdType"
              class="password"
              auto-complete="on"
              name="password"
              size="medium"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>

          <el-form-item>
            <el-button :loading="loading" :disabled="cancilck" class="login-button" type="primary" style="width: 100%" @click.native.prevent="handleLogin">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { Message } from 'element-ui'
// import store from '@/store'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 2) {
        callback(new Error('User ID不能小于2位'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      pwdType: 'password',
      redirect: undefined,
      showalert: false,
      cancilck: false
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },

  created() {
    // console.log(window.location.href, 'window.location.href')
    const a = window.location.href
    const arr = a.split('.')
    // console.log(arr.indexOf("walmartmobile"))
    if (arr.indexOf('walmartmobile') !== -1) {
      this.cancilck = true
      this.showalert = true
    } else {
      this.showalert = false
    }
  },

  methods: {
    handleLogin2() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('Login', this.loginForm)
            .then(
              () => {
                this.loading = false
                this.$router.push({ path: '/' })
              },
              (e) => {
                if (typeof e === 'string') {
                  Message({
                    message: e,
                    type: 'warning'
                  })
                }
                this.loading = false
              }
            )
            .catch((e) => {
              /* Message({
                                message: e,
                                type: 'warning'
                            })*/
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    // usercenter
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('userLogin', this.loginForm)
            .then(
              () => {
                this.loading = false
                this.$router.push({ path: '/' })
              },
              (e) => {
                if (typeof e === 'string') {
                  Message({
                    message: e,
                    type: 'warning'
                  })
                }
                this.loading = false
              }
            )
            .catch((e) => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    makesure() {
      this.cancilck = false
      window.location.href = 'http://aloha.cn.wal-mart.com:8080/wms-front/'
    },
    cancle() {
      this.showalert = false
      this.cancilck = false
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-container-background {
  background-image: url('../../assets/image/background_login.png');
  background-repeat: no-repeat;
  background-size: cover;
}

.login-body {
  display: flex;
  flex-direction: row;
}

.login-body-logo {
  width: 435px;
  height: 525px;
}

.login-body-form {
  width: 465px;
  height: 525px;
  background: #fff;
  border-radius: 0 30px 0 0;
  display: flex;
  flex-direction: column;
  padding-left: 66px;
  padding-right: 66px;
}

.title {
  margin-top: 54px;
  text-align: center;
  font-weight: bold;
  font-size: 26px;
}

.login-form {
  display: flex;
  flex-direction: column;
}

.username-tips {
  font-size: 16px;
  color: #828282;
  margin-top: 62px;
}

.mobile {
  margin-top: 7.5px;
}

.password-tips {
  font-size: 16px;
  color: #828282;
  margin-top: 20px;
}

.username {
  margin-top: 7.5px;
}

.login-button {
  margin-top: 42px;
  height: 46px;
  font-size: 16px;
}

.loginalertstyle {
  padding: 10px;
  position: absolute;
  width: 426px;
  height: 300px;
  top: 30%;
  background-color: white;
  color: black;
  border-radius: 10px;
  /*line-height: 300px;*/
  text-align: left;
}

.loginbtnsure {
  width: 140px;
  position: absolute;
  bottom: 10px;
  right: 70px;
}

.loginbtncancle {
  width: 140px;
  position: absolute;
  bottom: 10px;
  left: 70px;
}

.spanstyle {
  display: inline-block;
  height: 20px;
  line-height: 20px;
}
</style>

<style>
.login-container .el-input--medium .el-input__inner {
  height: 42px;
  line-height: 42px;
  font-size: 16px;
  background: #f8f8f8;
}
</style>
